﻿@using DeviceMaintainGIS.Model.DevInfo;
@{
    Layout = null;
    List<DevTabDeviceLevel> DevTabDeviceLevelList = ViewBag.DevTabDeviceLevelList;
}

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>设备信息</title>
    <link href="~/Scripts/jquery-easyui-1.5.4/themes/bootstrap/easyui.css" rel="stylesheet" />
    <script src="~/Scripts/jquery-easyui-1.5.4/jquery.min.js"></script>
    <script src="~/Scripts/jquery-easyui-1.5.4/jquery.easyui.min.js"></script>
    <script src="~/Scripts/jquery-easyui-1.5.4/locale/easyui-lang-zh_CN.js"></script>
    <link href="~/Content/themes/icon.css" rel="stylesheet" />
    <script src="~/Content/js/common.js"></script>
    <link href="~/Content/themes/table.css" rel="stylesheet" />
    <script type="text/javascript">
        $(function () {
            $.ajax({     //请求当前用户可以操作的按钮
                url: "/Button/GetUserAuthorizeButton?r=" + Math.random(),
                type: "post",
                data: { "KeyCode": "Dev", "KeyName": "Dev" },
                dataType: "json",
                timeout: 5000,
                success: function (data) {
                    if (data.success) {
                        var toolbar = getToolBar(data);      //common.js
                        if (data.search) {     //判断是否有浏览权限
                            var oldSelectRoleId;   //上次点选的RoleId
                            $("#ui_Dev_dg").datagrid({
                                url: "/DevInfo/GetAllDevInfo",
                                striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: true,
                                idField: 'ID',
                                //sortName: 'UpdateTime',
                                //sortOrder: 'desc',
                                pageList: [20, 40, 60, 80, 100],
                                frozenColumns: [[
                                                 //{ width: 100, title: '设备编号', field: 'DeviceId' },
                                                 { width: 100, title: '设备名称', field: 'DeviceName' },
                                                 { width: 100, title: '设备类别', field: 'DeviceTypeName' },
                                                 { width: 100, title: '厂家序列号', field: 'FactorySerial' },
                                                 { width: 100, title: '公司序列号', field: 'CompanySerial' },
                                                 { width: 100, title: '厂家规格型号', field: 'FModelSize' },
                                                 { width: 100, title: '公司规格型号', field: 'CModelSize' },
                                                 { width: 100, title: '采购负责人', field: 'Purchaser' },
                                                 {
                                                     width: 100, title: '采购时间', field: 'PurchaseTime', formatter: function (date) {
                                                         var pa = /.*\((.*)\)/;
                                                         var unixtime = date.match(pa)[1].substring(0, 10);
                                                         return getTime(unixtime);
                                                     }
                                                 },
                                                 {
                                                     width: 100, title: '质保期', field: 'WarrantyTime', formatter: function (date) {
                                                         var pa = /.*\((.*)\)/;
                                                         var unixtime = date.match(pa)[1].substring(0, 10);
                                                         return getTime(unixtime);
                                                     }
                                                 },
                                                 { width: 100, title: '设备厂家', field: 'DeviceFactory' },
                                                 { width: 100, title: '联系电话', field: 'DeviceTel' },
                                                 {
                                                     width: 100, title: '状态', field: 'State',
                                                     formatter: function (value, rec) {
                                                         if (value == 0) {
                                                             return '未出库';
                                                         }
                                                         else if (value == 1) {
                                                             return "已出库";
                                                         }
                                                         else {
                                                             return "报废";
                                                         }
                                                     }
                                                 },

                                ]],

                                toolbar: toolbar.length == 0 ? null : toolbar,
                                onSelect: function (rowIndex, rowData) {
                                    //alert(rowData.ID);
                                    $("#strLocaId").val(rowData.ID);
                                    if (oldSelectRoleId == rowData.ID) {
                                        return;
                                    }
                                    oldSelectRoleId = rowData.DeviceGuid;
                                    var $ui_role_layout = $("#ui_Dev_layout");
                                    var eastRoleUser = $ui_role_layout.layout("panel", "south");
                                    if (eastRoleUser.panel("options").collapsed) {   //判断是否展开
                                        $ui_role_layout.layout("expand", "south");
                                    }
                                    eastRoleUser.panel("setTitle", "子设备信息");
                                    if ($("#ui_Dev_son_dg").data("datagrid")) {
                                        $("#ui_Dev_son_dg").datagrid({       //请求数据
                                            url: "/DevInfo/GetDevInfoByDeviceGuid?ID=" + rowData.ID
                                        });
                                    }
                                    else {
                                        $("#ui_Dev_son_dg").datagrid({       //初始化datagrid
                                            url: "/DevInfo/GetDevInfoByDeviceGuid?ID=" + rowData.ID,
                                            striped: true, rownumbers: true, pagination: true, pageSize: 20, singleSelect: true,
                                            idField: 'ID',
                                            pageList: [20, 40, 60, 80, 100],
                                            columns: [[
                                                 //{ width: 100, title: '设备编号', field: 'DeviceId' },
                                                 { width: 100, title: '设备名称', field: 'DeviceName' },
                                                 { width: 100, title: '设备类别', field: 'DeviceTypeName' },
                                                 { width: 100, title: '厂家序列号', field: 'FactorySerial' },
                                                 { width: 100, title: '公司序列号', field: 'CompanySerial' },
                                                 { width: 100, title: '厂家规格型号', field: 'FModelSize' },
                                                 { width: 100, title: '公司规格型号', field: 'CModelSize' },
                                                 { width: 100, title: '采购负责人', field: 'Purchaser' },
                                                 {
                                                     width: 100, title: '采购时间', field: 'PurchaseTime', formatter: function (date) {
                                                         var pa = /.*\((.*)\)/;
                                                         var unixtime = date.match(pa)[1].substring(0, 10);
                                                         return getTime(unixtime);
                                                     }
                                                 },
                                                 {
                                                     width: 100, title: '质保期', field: 'WarrantyTime', formatter: function (date) {
                                                         var pa = /.*\((.*)\)/;
                                                         var unixtime = date.match(pa)[1].substring(0, 10);
                                                         return getTime(unixtime);
                                                     }
                                                 },
                                                 { width: 100, title: '设备厂家', field: 'DeviceFactory' },
                                                 { width: 100, title: '联系电话', field: 'DeviceTel' },
                                                 {
                                                     width: 100, title: '状态', field: 'State',
                                                     formatter: function (value, rec) {
                                                         if (value == 0) {
                                                             return '未出库';
                                                         }
                                                         else if (value == 1) {
                                                             return "已出库";
                                                         }
                                                         else {
                                                             return "报废";
                                                         }
                                                     }
                                                 },
                                            ]]
                                        });
                                    }
                                }
                            });
                        }
                        else {
                            $.show_alert("提示", "无权限，请联系管理员！");
                        }
                    } else {
                        $.show_alert("错误", data.result);
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    if (textStatus == "timeout") {
                        $.show_alert("提示", "请求超时，请刷新当前页重试！");
                    }
                    else {
                        $.show_alert("错误", textStatus + "：" + errorThrown);
                    }
                }
            })
        });

        //添加设备信息
        function AddDev() {
            $("<div/>").dialog({
                id: "ui_Dev_add_dialog",
                href: "/DevInfo/DevAdd",
                title: "添加设备信息",
                height: 570,
                width: 910,
                modal: true,
                buttons: [{
                    id: "ui_Dev_add_btn",
                    text: '添 加',
                    handler: function () {
                        //alert($('#ParentDev').combobox('getValue'));
                        if ($("#txtDeviceTypeId").val() == '0') {
                            alert('请选择设备类别！');
                            return false;
                        }
                        $("#DevAddForm").form("submit", {
                            url: "/DevInfo/DevInfoAdd",
                            onSubmit: function (param) {
                                //param.DeviceId = $("#txtDeviceId").val();
                                param.DeviceName = $("#txtDeviceName").val();
                                param.DeviceTypeId = $("#txtDeviceTypeId").val();
                                param.DeviceLevel = $("#txtDeviceLevel").val();
                                param.FactorySerial = $("#txtFactorySerial").val();
                                param.CompanySerial = $("#txtCompanySerial").val();
                                param.FModelSize = $("#txtFModelSize").val();
                                param.CModelSize = $("#txtCModelSize").val();
                                param.Software = $("#txtSoftware").val();
                                param.SoftwareVersion = $("#txtSoftwareVersion").val();
                                param.Purchaser = $("#txtPurchaser").val();
                                param.PurchaseTime = $("#txtPurchaseTime").datetimebox("getValue");
                                param.WarrantyTime = $("#txtWarrantyTime").datetimebox("getValue");
                                param.DeviceFactory = $("#txtDeviceFactory").val();
                                param.DeviceTel = $("#txtDeviceTel").val();
                                param.RepairFactory = $("#txtRepairFactory").val();
                                param.RepairTel = $("#txtRepairTel").val();
                                param.RepairFactoryManager = $("#txtRepairFactoryManager").val();
                                param.ParentDevID = $('#ParentDev').combobox('getValue');
                                if ($(this).form('validate')) {
                                    $('#ui_Dev_add_btn').linkbutton('disable');
                                    return true;
                                }
                                else {
                                    $('#ui_Dev_add_btn').linkbutton('enable');   //恢复按钮
                                    return false;
                                }
                            },
                            success: function (data) {
                                var dataJson = eval('(' + data + ')');
                                if (dataJson.success) {
                                    $("#ui_Dev_add_dialog").dialog('destroy');
                                    $.show_alert("提示", dataJson.msg);
                                    $("#ui_Dev_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                    window.location.reload();
                                } else {
                                    $('#ui_Dev_add_btn').linkbutton('enable');
                                    $.show_alert("提示", dataJson.msg);
                                }
                            }
                        });
                    }
                }, {
                    text: '取 消',
                    handler: function () {
                        $("#ui_Dev_add_dialog").dialog('destroy');
                    }
                }],
                onLoad: function () {
                    $("#txtDeviceId").focus();
                    $('#ParentDev').combobox({
                        prompt: '输入首关键字自动检索',
                        required: false,
                        url: '/DevInfo/GetSelDevInfo?DeviceLevel=' + $('#addselDeviceLevel').val(),
                        editable: true,
                        hasDownArrow: true,
                        filter: function (q, row) {

                            var opts = $(this).combobox('options');

                            return row[opts.textField].indexOf(q) >= 0;
                        }
                    });

                    $("#addselDeviceLevel").change(function () {
                        $('#ParentDev').combobox({
                            prompt: '输入首关键字自动检索',
                            required: false,
                            url: '/DevInfo/GetSelDevInfo?DeviceLevel=' + $('#addselDeviceLevel').val(),
                            editable: true,
                            hasDownArrow: true,
                            filter: function (q, row) {

                                var opts = $(this).combobox('options');

                                return row[opts.textField].indexOf(q) >= 0;
                            }
                        });
                    });
                },
                onClose: function () {
                    $("#ui_Dev_add_dialog").dialog('destroy');  //销毁dialog对象
                }
            });
        }

        //修改设备
        function EditDev() {
            //var rows = $("#ui_Dev_dg").datagrid("getChecked");
            //if (rows.length < 1) {
            //    $.show_alert("提示", "请先选择要修改的设备");
            //    return;
            //}
            //if (rows.length > 1) {
            //    $.show_alert("提示", "不支持批量修改设备");
            //    $("#ui_user_dg").datagrid('clearSelections').datagrid('clearChecked');
            //    return;
            //}
            //var id = "";
            //$.each(rows, function (i, row) {
            //    id += row.ID + ",";
            //});

            //id = id.substring(0, id.length - 1);
            //alert(id);
            var rows = $("#ui_Dev_dg").datagrid("getSelected");
            //alert(rows);

            if (rows == null) {
                $.show_alert("提示", "请先选择要修改的设备");
                return;
            }
            //alert(rows.State);
            if (rows.State != 0) {
                $.show_alert("提示", "出库或报废设备不能编辑");
                return;
            }

            var id = rows.ID;
            $("<div/>").dialog({
                id: "ui_Dev_edit_dialog",
                href: "/DevInfo/DevEdit?ID=" + id,
                title: "修改设备信息",
                height: 570,
                width: 910,
                modal: true,
                buttons: [{
                    id: "ui_Dev_edit_btn",
                    text: '修 改',
                    handler: function () {
                        if ($("#txtDeviceTypeId").val() == '0') {
                            alert('设备类别');
                            return false;
                        }
                        $("#DevEditForm").form("submit", {
                            url: "/DevInfo/DevInfoEdit",
                            onSubmit: function (param) {
                                param.ID = $("#txtID").val();
                                //param.DeviceId = $("#txtDeviceId").val();
                                param.DeviceName = $("#txtDeviceName").val();
                                param.DeviceTypeId = $("#txtDeviceTypeId").val();
                                param.DeviceLevel = $("#txtDeviceLevel").val();
                                param.FactorySerial = $("#txtFactorySerial").val();
                                param.CompanySerial = $("#txtCompanySerial").val();
                                param.FModelSize = $("#txtFModelSize").val();
                                param.CModelSize = $("#txtCModelSize").val();
                                param.Software = $("#txtSoftware").val();
                                param.SoftwareVersion = $("#txtSoftwareVersion").val();
                                param.Purchaser = $("#txtPurchaser").val();
                                param.PurchaseTime = $("#txtPurchaseTime").datetimebox("getValue");
                                param.WarrantyTime = $("#txtWarrantyTime").datetimebox("getValue");
                                param.DeviceFactory = $("#txtDeviceFactory").val();
                                param.DeviceTel = $("#txtDeviceTel").val();
                                param.RepairFactory = $("#txtRepairFactory").val();
                                param.RepairTel = $("#txtRepairTel").val();
                                param.RepairFactoryManager = $("#txtRepairFactoryManager").val();
                                param.ParentDevID = $('#ParentDev').combobox('getValue');
                                if ($(this).form('validate')) {
                                    $('#ui_Dev_edit_btn').linkbutton('disable');
                                    return true;
                                }
                                else {
                                    $('#ui_Dev_edit_btn').linkbutton('enable');   //恢复按钮
                                    return false;
                                }
                            },
                            success: function (data) {
                                var dataJson = eval('(' + data + ')');
                                if (dataJson.success) {
                                    $("#ui_Dev_edit_dialog").dialog('destroy');
                                    $.show_alert("提示", dataJson.msg);
                                    $("#ui_Dev_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                    window.location.reload()
                                } else {
                                    $('#ui_Dev_edit_btn').linkbutton('enable');
                                    $.show_alert("提示", dataJson.msg);
                                }
                            }
                        });
                    }
                }, {
                    text: '取 消',
                    handler: function () {
                        $("#ui_Dev_edit_dialog").dialog('destroy');
                    }
                }],
                onLoad: function () {
                    $("#txtDeviceId").focus();
                    $('#ParentDev').combobox({
                        prompt: '输入首关键字自动检索',
                        required: false,
                        url: '/DevInfo/GetSelDevInfo?DeviceLevel=' + $('#editselDeviceLevel').val(),
                        editable: true,
                        hasDownArrow: true,
                        filter: function (q, row) {

                            var opts = $(this).combobox('options');

                            return row[opts.textField].indexOf(q) >= 0;
                        }
                    });
                    $("#editselDeviceLevel").change(function () {
                        $('#ParentDev').combobox({
                            prompt: '输入首关键字自动检索',
                            required: false,
                            url: '/DevInfo/GetSelDevInfo?DeviceLevel=' + $('#editselDeviceLevel').val(),
                            editable: true,
                            hasDownArrow: true,
                            filter: function (q, row) {

                                var opts = $(this).combobox('options');

                                return row[opts.textField].indexOf(q) >= 0;
                            }
                        });
                    });
                },
                onClose: function () {
                    $("#ui_Dev_edit_dialog").dialog('destroy');  //销毁dialog对象
                }
            });
        }

        //删除设备
        function DelDev() {
            var rows = $("#ui_Dev_dg").datagrid("getSelected");
            if (rows.length < 1) {
                $.show_alert("提示", "请先选择要删除的设备");
                return;
            }
            //alert(rows.State);
            if (rows.State != 0) {
                $.show_alert("提示", "出库或报废设备不能删除");
                return;
            }
            $.messager.confirm('提示', '删除选中设备后，所有下级设备将被删除，确定删除选中设备吗？', function (r) {
                if (r) {
                    var ID = rows.ID;
                    //alert(ID);
                    //return;
                    //$.each(rows, function (i, row) {
                    //    ID += row.ID + ",";
                    //});

                    //ID = ID.substring(0, parmIds.length - 1);
                    $.ajax({
                        url: "/DevInfo/DevInfoDel",
                        data: {
                            ID: ID
                        },
                        type: "POST",
                        dataType: "json",
                        success: function (data) {
                            if (data.success) {
                                $.show_alert("提示", data.msg);
                                $("#ui_Dev_dg").datagrid("reload").datagrid('clearSelections').datagrid('clearChecked');
                                window.location.reload()
                            } else {
                                $.show_alert("提示", data.msg);
                            }
                        }
                    });
                }
            });
        }




        function ui_Dev_searchdata() {


            validateDateTime();

            $("#ui_Dev_dg").datagrid('load', {
                selDeviceName: $('#selDeviceName').val(),
                selCompanySerial: $('#selCompanySerial').val(),
                selCModelSize: $('#selCModelSize').val(),
                selStartWarrantyTime: $('#selStartWarrantyTime').val(),
                selEndWarrantyTime: $('#selEndWarrantyTime').val(),
                selDeviceLevel: $('#selDeviceLevel').val(),
                selDevState: $('#selDevState').val(),
            });
            $("#ui_Dev_dg").datagrid('clearSelections').datagrid('clearChecked');
        }

        function ui_Dev_cleardata() {

            $('#selDeviceName').val(''),
           $('#selCompanySerial').val(''),
            $('#selCModelSize').val(''),
           $('#selStartWarrantyTime').val(''),
           $('#selDevState').val('0'),
            $('#selDeviceLevel').val('1'),
            $('#selStartWarrantyTime').datebox('setValue', '');
            $('#selEndWarrantyTime').datebox('setValue', '');
            $("#ui_Dev_dg").datagrid('clearSelections').datagrid('clearChecked');
        }

        //时间转换
        function getTime(/** timestamp=0 **/) {
            var ts = arguments[0] || 0;
            var t, y, m, d, h, i, s;
            t = ts ? new Date(ts * 1000) : new Date();
            y = t.getFullYear();
            m = t.getMonth() + 1;
            d = t.getDate();
            h = t.getHours();
            i = t.getMinutes();
            s = t.getSeconds();
            // 可根据需要在这里定义时间格式
            return y + '-' + (m < 10 ? '0' + m : m) + '-' + (d < 10 ? '0' + d : d);
        }

        function validateDateTime() {
            console.log("hello world");

            var v1 = $("#selStartWarrantyTime").datetimebox('getValue');
            var date1 = new Date(v1);
            console.log(date1);
            var v2 = $("#selEndWarrantyTime").datetimebox('getValue');
            var date2 = new Date(v2);
            console.log(date2);
            if (v1 == '' || v2 == '') {
                return true;
            }
            console.log(date1);
            console.log(date2);
            if (date1 < date2) {
                console.log(date1 + "<" + date2);
                //var diff = DateDiff(date1, date2);
                //if (diff > 30) {
                //    $.messager.alert('提示', '时间段不能超过30天！');
                //}
                return true;
            }
            console.log(date1 + "!<" + date2);

            $.messager.alert('提示', '开始时间要小于结束时间！');

            return false;
        }


    </script>


</head>
<body>
    <div id="ui_Dev_layout" class="easyui-layout" data-options="fit:true,border:false">
        <div data-options="region:'north',split:true,border:true,collapsed:false" title="搜索条件" style="width: 100%;  height: 15%;">
            <div id="ui_user_search">
                <table class="tableForm" style="width: 100%; height: 100%; background: #F5F5F5;">
                    <tr>
                        <th>
                            设备名称：
                        </th>
                        <td>
                            <input name="selDeviceName" id="selDeviceName" class="easyui-validatebox textbox" style="width:110px;height:22px;" />
                        </td>

                        <th>
                            公司序列号：
                        </th>
                        <td>
                            <input name="selCompanySerial" id="selCompanySerial" class="easyui-validatebox textbox" style="width:110px;height:22px;" />
                        </td>
                        <th>
                            质保期：
                        </th>
                        <td>
                            开始时间：
                            <input name="selStartWarrantyTime" id="selStartWarrantyTime" class="easyui-datebox"
                                   editable="false" style="width:100px;height:22px;" />
                        </td>
                        <td>
                            结束时间：
                            <input name="selEndWarrantyTime" id="selEndWarrantyTime" class="easyui-datebox"
                                   editable="false" style="width:100px;height:22px;" />
                        </td>
                    </tr>
                    <tr>
                        <th>
                            公司规格型号：
                        </th>
                        <td>
                            <input name="selCModelSize" id="selCModelSize" class="easyui-validatebox textbox" style="width:110px;height:22px;" />
                        </td>


                        <th>
                            设备等级：
                        </th>
                        <td>
                            <select id="selDeviceLevel">
                                @foreach (DevTabDeviceLevel ltm in DevTabDeviceLevelList)
                                {

                                    <option value="@ltm.Level">@ltm.Level</option>

                                }
                            </select>
                        </td>
                        <th>
                            设备状态：
                        </th>
                        <td>
                            <select id="selDevState">
                                <option value="0" selected="selected">未出库</option>
                                <option value="1">已出库</option>
                                <option value="2">报废</option>
                            </select>
                        </td>
                        <td colspan="3">
                            <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-search" plain="true"
                               onclick="ui_Dev_searchdata();">搜索</a>
                            <a href="javascript:void(0);" class="easyui-linkbutton" iconcls="icon-clear" plain="true"
                               onclick="ui_Dev_cleardata();">清空条件</a>
                        </td>
                    </tr>
                </table>
            </div>
        </div>

        <div data-options="region:'south',split:true,border:true,collapsed:true" title="子设备信息"
             style="width: 480px;height:350px">
            <table id="ui_Dev_son_dg" data-options="fit:true,border:false"></table>
        </div>
        <div data-options="region:'center',border:false">
            <table id="ui_Dev_dg" data-options="fit:true,border:false"></table>
        </div>
    </div>
</body>
</html>